package com.itheima.behaviour.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.behaviour.dto.LikesBehaviourDto;
import com.itheima.behaviour.dto.UpdateArticleMess;
import com.itheima.behaviour.mapper.ApBehaviorEntryMapper;
import com.itheima.behaviour.mapper.ApLikesBehaviorMapper;
import com.itheima.behaviour.pojo.ApBehaviorEntry;
import com.itheima.behaviour.pojo.ApLikesBehavior;
import com.itheima.behaviour.service.ApLikesBehaviorService;
import com.itheima.common.constants.BusinessConstants;
import com.itheima.common.constants.SystemConstants;
import com.itheima.common.util.RequestContextUtil;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @description <p>APP点赞行为 业务实现</p>
 *
 * @version 1.0
 * @package com.itheima.behaviour.service.impl
 */
@Service
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior> implements ApLikesBehaviorService {

    @Resource
    private ApBehaviorEntryMapper apBehaviorEntryMapper;

    @Resource
    private KafkaTemplate kafkaTemplate;

    /**
     * 对文章的点赞行为
     *
     * @param dto
     */
    @Override
    public void likeBehaviour(LikesBehaviourDto dto) {
        //1. 判断登录类型
        int type = 1; //实体类型, 1: 用户

        Long userId = RequestContextUtil.getUserId();
        Long entryId = userId;
        if(SystemConstants.ANONYMOUS_USER_ID == userId){
            // 是匿名, 是设备
            entryId = dto.getEquipmentId().longValue();
            type = 0;
        }
        //2. 查询实体对象
        ApBehaviorEntry behaviorEntry = apBehaviorEntryMapper.findByEntryIdAndType(entryId, type);
        //3. 查询是否存在点赞行为
        ApLikesBehavior likesBehavior = query().eq("entry_id", behaviorEntry.getId())
            .eq("article_id", dto.getArticleId())
            .eq("type", dto.getType()).one();
        //4. 存在，则需要更新点赞类型
        if(null != likesBehavior){
            ApLikesBehavior updatePojo = new ApLikesBehavior();
            updatePojo.setOperation(dto.getOperation());
            updatePojo.setId(likesBehavior.getId());
            updateById(updatePojo);
        }else {
            //5. 不存在，则添加点赞记录
            likesBehavior = new ApLikesBehavior();
            likesBehavior.setOperation(dto.getOperation());
            likesBehavior.setEntryId(behaviorEntry.getId());
            likesBehavior.setArticleId(dto.getArticleId());
            likesBehavior.setType(dto.getType());
            likesBehavior.setCreatedTime(LocalDateTime.now());

            save(likesBehavior);
        }

        // 构建点赞消息
        UpdateArticleMess msg = new UpdateArticleMess();
        msg.setArticleId(dto.getArticleId());
        msg.setNum(dto.getOperation()==0?-1:1);
        msg.setType(UpdateArticleMess.UpdateArticleType.LIKES);

        // 发消息
        String json = JSON.toJSONString(msg);
        kafkaTemplate.send(BusinessConstants.MqConstants.HOT_ARTICLE_SCORE_TOPIC, json);
    }
}
